package com.xzcedu.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author 徐州城
 * @version 1.0
 */
@SuppressWarnings("all")
public class SocketTCP02Server {
    public static void main(String[] args) throws IOException {
        //思路
        //1. 在本机 9999端口，等待连接
        //说明：要求在本机没有其它的服务在监听 9999
        //说明：这个 ServerSocket 可以通过 accept() 返回多个Socket[多个客户端连接服务器的并发]
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务端，在9999端口监听，等待连接");
        //2. 当没有客户端连接9999端口时，程序会阻塞，等待连接
        //如果有客户端连接，则会返回一个Socket对象
        Socket socket = serverSocket.accept();
        System.out.println("socket=" + socket.getClass());
        //3. 通过 socket.getInputStream() 读取客户端写入到数据通道的数据，显示
        InputStream inputStream = socket.getInputStream();
        //4. IO读取
        byte[] buf = new byte[1024];
        int readLen = 0;

        while ((readLen = inputStream.read(buf)) != -1) {
            System.out.println(new String(buf, 0, readLen));//根据读取到的实际长度，显示内容
        }
        socket.shutdownInput();

        //5. 获取sokect相关的输出流
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("Hello Clent".getBytes());
        socket.shutdownOutput();

        //6. 关闭流和socket
        inputStream.close();
        outputStream.close();
        socket.close();
        serverSocket.close();//关闭
    }
}
